package com.dongdongshop.controller;

import com.dongdongshop.data.Result;
import com.dongdongshop.enums.ResultEnum;
import com.dongdongshop.page.PageResult;
import com.dongdongshop.pojo.TbBrand;
import com.dongdongshop.service.TbBrandService;
import org.apache.dubbo.config.annotation.DubboReference;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.List;


@Controller
@RequestMapping("brand")
public class TbBrandController {

    private Logger logger = LoggerFactory.getLogger(TbBrandController.class);
    @DubboReference
    private TbBrandService brandService;

    //跳转页面方法
    @RequestMapping("show")
    public String show(){
        return "admin/brand";
    }

    //查询所有方法
    @RequestMapping("listBrands")
    @ResponseBody
    public Result<TbBrand> listBrands(@RequestParam(defaultValue = "1")Integer pageNum,
                                      @RequestParam(defaultValue = "5")Integer pageSize){
        try {
            PageResult result = brandService.listBrands(pageNum,pageSize);
            Result result1 = Result.OK().setData(result);
            return result1;
        }catch (Exception e){
            logger.info("查询所有方法异常 {}",e.toString());
            return Result.response(ResultEnum.SELECT_MESSAGE_ERROE);
        }
    }
    //增加方法
    @RequestMapping("addBrand")
    @ResponseBody
    public Result addBrand(TbBrand brand){
        try {
            brandService.addBrand(brand);
            return Result.OK();
        }catch (Exception e){
            logger.info("增加方法异常 {}",e.toString());
            return Result.response(ResultEnum.ADD_MESSAGE_ERROE);
        }
    }
    //回显方法
    @RequestMapping("toUpdateBrand")
    @ResponseBody
    public Result toUpdatebrand(Integer id){
        try {
            TbBrand brand = brandService.selectBrandById(id);
            return Result.OK().setData(brand);
        }catch (Exception e){
            logger.info("回显方法异常 {}",e.toString());
            return Result.response(ResultEnum.SELECT_MESSAGE_ERROE);
        }
    }
    //修改方法
    @RequestMapping("updateBrand")
    @ResponseBody
    public Result updatebrand(TbBrand brand){
        try {
            brandService.updateBrandById(brand);
            return Result.OK();
        }catch (Exception e){
            logger.info("修改方法异常 {}",e.toString());
            return Result.response(ResultEnum.UPDATE_MESSAGE_ERROE);
        }
    }
    //删除单个fng
    @RequestMapping("deleteBrand")
    @ResponseBody
    public Result deleteBrand(Integer id){
        try {
            brandService.deleteBrand(id);
            return Result.OK();
        }catch (Exception e){
            logger.info("删除方法异常 {}",e.toString());
            return Result.response(ResultEnum.DELETE_MESSAGE_ERROE);
        }
    }
    @RequestMapping("deleteBrands")
    @ResponseBody
    public Result deleteBrands(Long[] ids){
        try {
            brandService.deleteBrands(ids);
            return Result.OK();
        }catch (Exception e){
            logger.info("批量删除方法异常 {}",e.toString());
            return Result.response(ResultEnum.DELETE_MESSAGE_ERROE);
        }
    }
}
